<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 10/8/14
 * Time: 9:28 PM
 */

namespace Backend\Modules\User\Controllers;
use Backend\Modules\User\Forms\FormCreateUser;
use Backend\Modules\User\Forms\FormLogin;
use Backend\Modules\User\Models\Users as Users;
use Backend\Modules\User\Models\UsersProfile as UsersProfile;

class ProfilesController extends \BackendController
{
    public function indexAction()
    {
        $userType = $this->request->get('user_type');
        $this->view->users = Users::find();
    }

    /*
     * create profile
     */
    public function createAction()
    {
        $form = new FormCreateUser();

        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost()) != false) {
                $user = new Users();

                $user->assign(array(
                    'name' => $this->request->getPost('name'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'group_id' => $this->request->getPost('group_id'),
                    'banned' => 'N',
                    'suspended' => 'N',
                    'active' => 'Y',
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s")
                ));

                if ($user->save()) {

                    $userProfiles = new UsersProfile();
                    $userProfiles->assign(array(
                        'users_id' => $user->getId(),
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s")
                    ));

                    $userProfiles->save();

                    return $this->dispatcher->forward(array(
                        'controller' => 'user',
                        'action' => 'index'
                    ));
                }

                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }


    public function editAction($id)
    {
        $profile = Users::findFirstById($id);
        if(!$profile)
        {
            $this->flash->error("user was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }

        $form = new FormCreateUser($profile , array('edit' => true));

        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost()) != false) {
                $user = new Users();

                $user->assign(array(
                    'name' => $this->request->getPost('name'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'group_id' => $this->request->getPost('group_id'),
                    'banned' => 'N',
                    'suspended' => 'N',
                    'active' => 'Y',
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s")
                ));

                if ($user->save()) {

                    $userProfiles = new UsersProfile();
                    $userProfiles->assign(array(
                        'users_id' => $user->getId(),
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s")
                    ));

                    $userProfiles->save();

                    return $this->dispatcher->forward(array(
                        'controller' => 'user',
                        'action' => 'index'
                    ));
                }

                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }
} 